package com.weifeiyang.aidlserverdemo;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {
    private String TGA = "MyService";
    private long startTime;
    private NotifyCallBack mCallBack;

    public MyService() {
    }

    /**
     * MyServiceAidl.Stub类实现了IBinder和IRemoteService接口
     * 因此Stub的子类对象可以作为onBinder()方法的返回值.
     *
     * @author xing
     */
    public class MyBinder extends MyServiceAidl.Stub {

        @Override
        public int getServiceRunTime() throws RemoteException {
            Log.e(TGA, "getServiceRunTime" + (System.currentTimeMillis() - startTime) + "");
            timer.schedule(task, 0, 1000); // 0s后执行task,经过1s再次执行
            return Integer.valueOf(System.currentTimeMillis() - startTime + "");
        }

        @Override
        public String getData(String data) throws RemoteException {
            return "ceshi" + position;
        }

        @Override
        public String add(int a, int b) throws RemoteException {
            Log.e(TGA, "add" + a + "===" + b);
            return a + b + "";

        }

        @Override
        public void registerCallBack(NotifyCallBack cb) throws RemoteException {
            mCallBack = cb;
        }

        @Override
        public void unregisterCallBack(NotifyCallBack cb) throws RemoteException {
            //mCallBacks.unregister(cb);
        }

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TGA, "onCreate");
        startTime = System.currentTimeMillis();
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TGA, "onBind");
        return new MyBinder();
    }

    @Override
    public void onDestroy() {
        Log.e(TGA, "onDestroy");
        super.onDestroy();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TGA, "onUnbind");
        timer.cancel();
        position = 0;
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.e(TGA, "onRebind");
        super.onRebind(intent);
    }

    int position = 0;
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                position++;
                try {
                    Log.e(TGA, "这是第" + position + "个");
                    mCallBack.notifyMainUiThread("这是第" + position + "个");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
            super.handleMessage(msg);
        }
    };

    /**
     * 计时器
     */
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            // 需要做的事:发送消息
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }
    };


}
